<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kecamatan extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('Kecamatan_model','m');
    $this->load->helper('url');
	  $this->load->library('session');
  }


  //list semua Kecamatan
  public function index()
  {
 
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');
 
    //load library table agar mudah membuat tabel
    $this->load->library('table');

    $all_kecamatan = $this->m->all();
    $this->table->set_heading('ID', 'Nama', 'ID Polygon','');
    foreach($all_kecamatan as $kecamatan){
      $this->table->add_row(
        $kecamatan->id,
        $kecamatan->nama,
        $kecamatan->id_polygon,
        anchor('kecamatan/view/'.$kecamatan->id,'Lihat'),
        anchor('kecamatan/edit/'.$kecamatan->id,'Edit'),
        anchor('kecamatan/view_kelurahan/'. $kecamatan->id,'Kelurahan','title="Lihat semua kelurahan di kecamatan '.$kecamatan->nama. '"')
      );
    }
    echo $this->table->generate();

  }


  //test view untuk menampilkan peta
	public function index_map()
	{
    $data['title'] ='Kecamatan di Kota Pekanbaru';
    $data['js']='';
		$this->load->view('Kecamatan',$data);
	}


  /** Tambah Kecamatan*/
  public function add(){

	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

  
    //load helper form untuk memudahkan kita membuat form
    $this->load->helper('form');
    echo form_open('kecamatan/save');
    echo form_input('nama', '');
    echo form_submit('tambah', 'Tambah Kecamatan');
    echo form_close();
  }

  //create and save our new kecamatan
  public function save(){
    //save our kecamatan
    $data['nama']=$this->input->post('nama');
    $id_kecamatan = $this->m->insert($data);
		redirect('kecamatan/view/'.$id_kecamatan);

  }


  public function edit($id_kecamatan=1)
  {
 
	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

 
    $this->load->library('table');
    $this->load->helper('form');
    $kecamatan = $this->m->get($id_kecamatan);

    //start form
    echo form_open('kecamatan/update/'.$kecamatan->id);

    //add row to our table
    $this->table->add_row('id',':', $kecamatan->id . form_hidden('id',$kecamatan->id));
    $this->table->add_row('created_at',':', $kecamatan->created_at);
    $this->table->add_row('updated_at',':', $kecamatan->updated_at);
    $this->table->add_row('Nama Kecamatan',':',form_input('nama', $kecamatan->nama));
    

    $this->table->add_row(form_submit('simpan', 'Update Kecamatan'));
    $this->table->add_row(anchor('kecamatan','Kembali ke daftar Kecamatan'));
    echo $this->table->generate();

    //close our form
    echo form_close();

  }


  public function update($id_kecamatan){
    //update our kecamatan
    $data['nama']=$this->input->post('nama');
    $this->m->update($id_kecamatan,$data);
		redirect('kecamatan/view/'.$id_kecamatan);

  }

  public function view($id_kecamatan=1){
  
  	//user should login first if willing to see this page
	if(!$this->session->userdata('id_user')) redirect('user/login');

	//if not 9, not an admin :D
	if(8>$this->session->userdata('level')) 
		return redirect('user/home');

  
    $this->load->library('table');
    $kecamatan = $this->m->get($id_kecamatan);
    //header('Content-Type: text/plain');
    //var_dump($this->m->get($id_kecamatan));

    $this->table->add_row('id',':', $kecamatan->id);
    $this->table->add_row('created_at',':', $kecamatan->created_at);
    $this->table->add_row('updated_at',':', $kecamatan->updated_at);
    $this->table->add_row('Nama Kecamatan',':',$kecamatan->nama);

    $this->table->add_row(anchor('kecamatan/view_kelurahan/'. $kecamatan->id,'Lihat semua kelurahan di kecamatan ini'));

    $this->table->add_row(anchor('kecamatan/add','Tambah Kecamatan Baru'));
    $this->table->add_row(anchor('kecamatan','Kembali ke daftar Kecamatan'));
    echo $this->table->generate();

  }

  //list semua kelurahan yang ada di kecamatan ini
  public function view_kelurahan($id_kecamatan)
  {
    $result = $this->m->getAllKelurahanAsArray($id_kecamatan);
    //var_dump ($result);
    //array(4) { [1]=> string(12) "Simpang Tiga" [2]=> string(17) "Tangkerang Labuai" [3]=> string(18) "Tangkerang Selatan" [4]=> string(16) "Tangkerang Utara" }
    $this->load->library('table');
    foreach($result as $a){
      $this->table->add_row($a);

    }
    echo $this->table->generate();

  }

}
